package com.realtyManage.springboot.Service.impl;

import com.realtyManage.springboot.Config.MyLog;
import com.realtyManage.springboot.Dao.*;
import com.realtyManage.springboot.Model.Do.*;
import com.realtyManage.springboot.Model.Vo.BaseEntity;
import com.realtyManage.springboot.Model.Vo.CustomReEntity;
import com.realtyManage.springboot.Service.AreaService;
import com.realtyManage.springboot.Service.ProjectDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class ProjectDetailServiceImpl implements ProjectDetailService {
    @Autowired
    private ProjectDetailRepository projectDetailRepository;

    @Autowired
    private HouseDynamicRepository houseDynamicRepository;

    @Autowired
    private ProjectAssortRepository projectAssortRepository;

    @Autowired
    private ProjectExhibitionRepository projectExhibitionRepository;

    @Autowired
    private ProjectApartmentRepository projectApartmentRepository;

    @Autowired
    private IntegralRepository integralRepository;

    @Autowired
    private UserServiceImpl userServiceImpl;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserTaskRepository userTaskRepository;

    @Autowired
    private ProjectImgRepository projectImgRepository;

    @Autowired
    private DiscussRepository discussRepository;

    @Autowired
    private HouseDetailRepository houseDetailRepository;

    @Autowired
    private HomePurchasePlanRepository homePurchasePlanRepository;

    @Override
    public ProjectDetail save(ProjectDetail projectDetail) {
        return projectDetailRepository.saveAndFlush(projectDetail);
    }

    @Override
    public List<ProjectApartment> findPagedByWhereClause(ProjectApartment projectApartment) {
        //排序条件
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        //动态查询条件
        Specification<ProjectApartment> specification = (Specification<ProjectApartment>)(root, query, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();
            if (projectApartment.getPropertyType() != null && projectApartment.getPropertyType() != ""){
                Predicate p0 = criteriaBuilder.equal(root.get("propertyType"), projectApartment.getPropertyType());
                list.add(p0);
            }
            if (projectApartment.getHouseScale() != null && projectApartment.getHouseScale() != ""){
                Predicate p1 = criteriaBuilder.equal(root.get("houseScale"), projectApartment.getHouseScale());
                list.add(p1);
            }
            if (projectApartment.getHouseType() != null && projectApartment.getHouseType() != ""){
                Predicate p2 = criteriaBuilder.equal(root.get("houseType"), projectApartment.getHouseType());
                list.add(p2);
            }
            return criteriaBuilder.and(list.toArray(new Predicate[0]));
        };
        return projectApartmentRepository.findAll(specification);
    }

    @Override
    public int deleteHouseDynamic(List ids) {
        return houseDynamicRepository.deleteHouseDynamic(ids);
    }

    @Override
    public List<HouseDynamic> showNewHouseDynamic() {
        return projectDetailRepository.showNewHouseDynamic();
    }

    @Override
    public List<HouseDynamic> showHouseDynamic() {
        return houseDynamicRepository.showHouseDynamic();
    }

    @Override
    public ProjectDetail addOrUpdateProjectDetail(ProjectDetail projectDetail) {
        return projectDetailRepository.saveAndFlush(projectDetail);
    }

    @Override
    public ProjectAssort addProjectAssort(ProjectAssort projectAssort) {
        return projectAssortRepository.saveAndFlush(projectAssort);
    }

    @Override
    public int deleteProjectAssort(List ids) {
        return projectAssortRepository.deleteProjectAssort(ids);
    }

    @Override
    public List<ProjectExhibition> showProjectExhibition() {
        return projectExhibitionRepository.findAll();
    }

    @Override
    public List<ProjectAssort> queryPt(Integer type) {
        return projectAssortRepository.queryPt(type);
    }

    @Override
    public List<ProjectDetail> showProject() {
        return projectDetailRepository.findAll();
    }

    @Override
    public HouseDynamic addHouseDynamic(HouseDynamic houseDynamic) {
        return houseDynamicRepository.saveAndFlush(houseDynamic);
    }

    @Override
    public int updateHouseDynamic(HouseDynamic houseDynamic) {
        return houseDynamicRepository.updateHouseDynamic(houseDynamic);
    }


    @Override
    public int updateProjectExhibition(ProjectExhibition projectExhibition) {
        return projectExhibitionRepository.updateProjectExhibition(projectExhibition);
    }

    @MyLog(value = "新增项目户型")
    @Override
    public ProjectApartment addProjectApartment(ProjectApartment projectApartment) {
        return projectApartmentRepository.saveAndFlush(projectApartment);
    }

    @MyLog(value = "删除项目户型")
    @Override
    public int deleteProjectApartment(List ids) {
        return projectApartmentRepository.deleteProjectApartment(ids);
    }

    @Override
    public int updateProjectApartment(ProjectApartment projectApartment) {
        return projectApartmentRepository.updateProjectApartment(projectApartment);
    }

    @Override
    public List getCountHouseScale() {
        return projectApartmentRepository.getCountHouseScale();
    }

    @Override
    public Map getCountPropertyType() {
        LinkedHashMap ptMap = new LinkedHashMap();
        List lst = projectApartmentRepository.getCountPropertyType();
        for (int i = 0;i < lst.size();i++){
            ptMap.put(lst.get(i).toString(),projectApartmentRepository.getCountPropertyType(lst.get(i).toString()));
        }
        return ptMap;
    }

    @Override
    public int updateIntegral(Integral integral) {
        return integralRepository.updateIntegral(integral);
    }

    @MyLog(value = "获取积分列表")
    @Override
    public List<Integral> getIntegralList() {
        return integralRepository.findAll();
    }

    @Override
    public List getCountPublicCustomer() {
        return integralRepository.getCountPublicCustomer();
    }

    @Override
    public List<User> getCompleteTaskSalespersonList(String complete) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        //获取销售员信息
        Integer is = 0;
        Integer lfs = 0;
        Integer tzkhs = 0;
        Integer yydfs = 0;
        Integer hfkhs = 0;
        Integer rckhs = 0;
        Integer cjkhs = 0;
        Integer hkkhs = 0;
        Integer zfs = 0;
        String userIds = "";
        List<User> userList = userServiceImpl.queryUserNum();
        String date = sdf.format(new Date());
        for (int i = 0;i<userList.size();i++){
            is = 0; //初始化总数
            List<Object> task = integralRepository.getTask(userList.get(i).getId(),date);
            for (int j=0;j<task.size();j++) {
                Object[] objects = (Object[]) task.get(j);
                lfs = Integer.valueOf(objects[0].toString());
                tzkhs = Integer.valueOf(objects[1].toString());
                yydfs = Integer.valueOf(objects[2].toString());
                hfkhs = Integer.valueOf(objects[3].toString());
                rckhs = Integer.valueOf(objects[4].toString());
                cjkhs = Integer.valueOf(objects[5].toString());
                hkkhs = Integer.valueOf(objects[6].toString());
                zfs = Integer.valueOf(objects[7].toString());
            }
            Integer receptionNum = integralRepository.getReceptionNum(userList.get(i).getId(),date);   //获取接待客户数
            if (receptionNum >= lfs){
                is += 1;    //来访数完成任务
            }
            Integer tzNum = integralRepository.getTzNum(userList.get(i).getId(),date);   //获取销售员拓展数
            if (tzNum >= tzkhs){
                is += 1;  //拓展客户
            }
            Integer yyNum = integralRepository.getYyNum(userList.get(i).getId(),date);   //获取销售员邀约数
            if (yyNum >= yydfs){
                is += 1;
            }
            Integer hfNum = integralRepository.getHfNum(userList.get(i).getId(),date);   //获取销售员回访数
            if (hfNum >= hfkhs){
                is += 1;
            }
            Integer rcNum = integralRepository.getRcNum(userList.get(i).getId(),date);   //获取销售员认筹数
            if (rcNum >= rckhs){
                is += 1;
            }
            Integer cjNum = integralRepository.getCjNum(userList.get(i).getId(),date);   //获取销售员成交数
            if (cjNum >= cjkhs){
                is += 1;
            }
            Integer hkNum = integralRepository.getHkNum(userList.get(i).getId(),date);   //获取销售员回款数
            if (hkNum >= hkkhs){
                is += 1;
            }
            Integer zfNum = integralRepository.getZfNum(userList.get(i).getId(),date);   //获取销售员回款数
            if (zfNum >= zfs){
                is += 1;
            }
            if (is == 8){
                userIds += userList.get(i).getId()+",";
            }
        }
        List uid = new ArrayList();
        String ids[] = userIds.split(",");
        for (String id : ids){
            uid.add(id);
        }
        if (complete.equals("1")){
            return userRepository.queryUserByIds(uid);
        }else{
            return userRepository.queryNUserByIds(uid);
        }
    }

    @Override
    public List<UserTask> getInformal() {
        return userTaskRepository.getInformal();
    }

    @Override
    public UserTask queryTaskDetailById(Integer id) {
        return userTaskRepository.queryTaskDetailById(id);
    }

    @Override
    public List getCountImg() {
        return projectImgRepository.getCountImg();
    }

    @Override
    public List<ProjectImg> getImgByType(Integer type) {
        return projectImgRepository.getImgByType(type);
    }

    @Override
    public int updateProjectImgByType(ProjectImg projectImg) {
        return projectImgRepository.update(projectImg);
    }

    @Override
    public List<Discuss> getDiscussTree() {
        return discussRepository.getDiscussTree();
    }

    @Override
    public int deleteDiscuss(Integer id) {
        return discussRepository.deleteDiscuss(id);
    }

    @Override
    public List<Discuss> getAuditDiscuss() {
        return discussRepository.getAuditDiscuss();
    }

    @Override
    public int auditDiscuss(Integer id,String type) {
        if (type.equals("0")){
            //未通过
            return discussRepository.deleteDiscuss(id);
        }else{
            //通过
            return discussRepository.updateDiscuss(id);
        }
    }

    @Override
    public Discuss addDiscuss(Discuss discuss) {
        return discussRepository.save(discuss);
    }

    @Override
    public List getApartment() {
        return houseDetailRepository.getApartment();
    }

    @Override
    public List getApartmentNumberList(String code) {
        List len =  houseDetailRepository.getLen(code);
        List lst = houseDetailRepository.getList(code,len.size());
        return lst;
    }

    @Override
    public List<ProjectApartment> getPropertyTyeList(ProjectApartment projectApartment) {
        //排序条件
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        //动态查询条件
        Specification<ProjectApartment> specification = (Specification<ProjectApartment>)(root, query, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();
            if (projectApartment.getPropertyType() != null){
                Predicate p0 = criteriaBuilder.equal(root.get("propertyType"), projectApartment.getPropertyType());
                list.add(p0);
            }
            return criteriaBuilder.and(list.toArray(new Predicate[0]));
        };
        return projectApartmentRepository.findAll(specification);
    }

    @Override
    public List<String> getNameAndImg(CustomReEntity customReEntity) {
        List<String> list = new ArrayList<>();
        String img = null;
        //if (customReEntity.getQueryType().equals("0")){
        String imgArray = projectApartmentRepository.getImg(customReEntity.getApartmentNumber());
        /*if (imgArray.length() > 0){
        String imgAndFloor[] = imgArray.split(",");
            for (String im : imgAndFloor){
                System.out.println("============>"+im);
                if (im.contains(customReEntity.getFloor())){
                     img = im;
                }
            }
        }*/
        list.add(imgArray);
    //}/*else{
            //查询别墅置业计划书
       /*     List<ProjectApartment> bsImgArray = projectApartmentRepository.getBsImg(customReEntity.getApartmentNumber(),customReEntity.getApartmentStructure(),customReEntity.getFloor());
           if (bsImgArray.size() > 0){
            if (customReEntity.getFloor().equals("负二层")){
               // home_ownership_minus_one  bsImgArray.
                img = bsImgArray.get(0).getHomeOwnershipMinusTwo();
            }else if (customReEntity.getFloor().equals("负一层")){
                img = bsImgArray.get(0).getHomeOwnershipMinusOne();
            }else if (customReEntity.getFloor().equals("一层")){
                img = bsImgArray.get(0).getHomeOwnershipOne();
            }else if (customReEntity.getFloor().equals("二层")){
                img = bsImgArray.get(0).getHomeOwnershipTwo();
            }else if(customReEntity.getFloor().equals("三层")){
                img = bsImgArray.get(0).getHomeOwnershipThree();
            }else if (customReEntity.getFloor().equals("四层")){
                img = bsImgArray.get(0).getHomeOwnershipFour();
            }
            list.add(img);
           }
        }*/
        return list;
    }

    @Override
    public List<HouseDetail> getHouseDetailById(String id) {
        return houseDetailRepository.getHouseDetailById(id);
    }

    @Override
    public HomePurchasePlan addHomePurchasePlan(HomePurchasePlan homePurchasePlan) {
        return homePurchasePlanRepository.save(homePurchasePlan);
    }

    @Override
    public int deleteHomePurchasePlan(Integer id) {
         homePurchasePlanRepository.deleteById(id);
        return 1;
    }

    @Override
    public List<HomePurchasePlan> getHomePurchasePlan(String id) {
        return homePurchasePlanRepository.getHomePurchasePlan(id);
    }

    @Override
    public HomePurchasePlan getHomePurchasePlanDetail(String id) {
        return homePurchasePlanRepository.getOne(Integer.valueOf(id));
    }

    @Override
    public List<Discuss> getDiscussTreeDetailById(String id) {
        return discussRepository.getDiscussTreeDetailById(id);
    }

    @Override
    public List getNameAndPhone() {
        return userRepository.getNameAndPhone();
    }

}
